package fr.excilys.gwt.events.client.component;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

import fr.excilys.gwt.events.client.event.AddPhoneNumberEvent;
import fr.excilys.gwt.events.client.event.AddPhoneNumberHandler;
import fr.excilys.gwt.events.client.event.ExcilysEventBus;

public class PhoneNumberEditor extends Composite implements
		HasAddPhoneNumberHandlers {

	private final ExcilysEventBus eventBus = ExcilysEventBus.INSTANCE;

	public PhoneNumberEditor() {
		HorizontalPanel mainPanel = new HorizontalPanel();
		final TextBox phoneNumberInput = new TextBox();
		mainPanel.add(phoneNumberInput);
		Button addButton = new Button("Ajouter");
		addButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireEvent(new AddPhoneNumberEvent(phoneNumberInput.getText()));
				eventBus.fireEvent(new AddPhoneNumberEvent(phoneNumberInput
						.getText()));
			}
		});
		mainPanel.add(addButton);
		initWidget(mainPanel);
	}

	@Override
	public HandlerRegistration addAddPhoneNumberHandler(
			AddPhoneNumberHandler handler) {
		return addHandler(handler, AddPhoneNumberEvent.getType());
	}

}
